<template>
  <div>
    <el-dialog
      :title="'添加维修记录'"
      :visible.sync="addEditIsDialog_c"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="120px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="维修图片：" prop="repairImagesArr">
              <MultipleImgUpload
                sirProps="image"
                :sirUrl="ruleForm.repairImagesArr"
                :limit="4"
                @upSuccess="upSuccess"
                :disabled="seeType"
              ></MultipleImgUpload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import MultipleImgUpload from "@/components/MultipleImgUpload.vue";
import { saveDataRepairRecord } from "@/api/repair";

export default {
  components: {
    MultipleImgUpload
  },
  props: {
    recordId: {
      type: String,
      default: "",
    },
    recordStatus: {
      type: String,
      default: "add",
    },
    recordIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        repairImages: "",
        repairImagesArr: [],
      },
      rules: {
        // 表单规则
        repairImagesArr: [{ required: true, message: "请上传维修图片", trigger: "change" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
    };
  },
  watch: {
    recordIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.recordStatus == "add") {
          this.ruleForm.id = this.recordId + "";
          this.seeType = false;
        }
      }
    },
  },
  methods: {
    // 提交
    submitForm(formName) {
      this.ruleForm.repairImages = this.ruleForm.repairImagesArr.toString();
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveDataRepairRecord(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
  // 上传图片
  upSuccess(data) {
      this.ruleForm.repairImagesArr = data;
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}

.el-cascader {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
